﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Winwise.SPWebConfig.Core.CodeModification;
namespace TestModifByCode
{
    public class SuperModif : IWebConfigModificationHandler
    {
        //Add an appsettings
        public void ApplyWebConfigModification(System.Xml.XmlDocument doc, Microsoft.SharePoint.Administration.SPUrlZone zone, Microsoft.SharePoint.Administration.SPWebApplication webapp)
        {
            XmlNode node = doc.SelectSingleNode("configuration/appSettings");
            if (node == null)
            {
                XmlNode root = doc.SelectSingleNode("configuration");
                root.AppendChild(doc.CreateElement("appSettings"));
                node = doc.SelectSingleNode("configuration/appSettings");
            }
            var elt = doc.CreateElement("add");
            elt.SetAttribute("key", "MyKey");
            elt.SetAttribute("value", "MyValue");
            node.AppendChild(elt);
        }

        public void UnApplyWebConfigModification(System.Xml.XmlDocument doc, Microsoft.SharePoint.Administration.SPUrlZone zone, Microsoft.SharePoint.Administration.SPWebApplication webapp)
        {
            XmlNode node = doc.SelectSingleNode("configuration/appSettings");
            XmlNode nodetoremove = node.SelectSingleNode("add[@key='MyKey']");
            node.RemoveChild(nodetoremove);
        }
    }
}
